package com.hunk.taopeili;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 
 * 类描述：email工具
 * 
 * @author liming
 * @time 2011-7-19 下午02:51:56
 */
public class EmailUtils {

	public static boolean sendMsg(String name, String msg, String toMailAddress) {
		 String smtphost = "smtp.163.com"; // 发送邮件服务器
         String user = "niknow"; // 邮件服务器登录用户名
         String password = "5845123005845123"; // 邮件服务器登录密码
         String from = "niknow@163.com"; // 发送人邮件地址
         String to = toMailAddress; // 接受人邮件地址
         String subject = "Thanks For You FeedBack"; // 邮件标题
         String body = msg + " author: " + name; // 邮件内容
         Properties props = new Properties();
         props.put("mail.smtp.host", smtphost);
         props.put("mail.smtp.auth", "true");
         Session ssn = Session.getInstance(props, null);

         MimeMessage message = new MimeMessage(ssn);

         try {
             InternetAddress fromAddress = new InternetAddress(from);
             message.setFrom(fromAddress);
             InternetAddress toAddress = new InternetAddress(to);
             message.addRecipient(Message.RecipientType.TO, toAddress);

             message.setSubject(subject);
             message.setText(body);

             Transport transport = ssn.getTransport("smtp");
             transport.connect(smtphost, user, password);
             transport.sendMessage(message, message
                     .getRecipients(Message.RecipientType.TO));
             //transport.send(message);
             transport.close();
             System.out.println("mail has been send");
             return true;
         } catch (MessagingException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
		return false;
	}
	
    // 测试：发送邮件  
    public static void main(String[] args) throws Exception {  
    	sendMsg("Hunk", "Test Java Mail", "niknow@163.com");
    	sendMsg("Hunk", "Test Java Mail", "niknow@live.com");
    }  

}
